/**
 * Created by jocky on 2017/7/19.
 */

var agentObj = {
    /**
     * 代理用户列表
     * @param type
     * @param direction
     */
    lists: function (type, direction) {
        //显示loading
        show_loading_table('agent_lists_table', 8);

        var params = {};
        params.isRender = 2;//ajax调用

        var pageNo_dom = 'agent_lists_pageNo';
        var pageTotal_dom = 'agent_lists_pageTotal';
        params.pageNo = getPageNo(pageNo_dom, pageTotal_dom, type, direction);

        $.each($('#agent_search_form').find('input,select'), function (i, v) {
            var name = $.trim($(v).attr('name'));
            if (name != '') {
                params[name] = $(v).val();
            }
        });

        $.getJSON('/agent/lists', params, function (json) {
            $("#" + pageNo_dom).val(parseInt(json.pageNo));
            $("#" + pageTotal_dom).html(parseInt(json.pageTotal));

            var tr = "";
            $.each(json.lists, function (i, v) {
                // console.log();
                if (v.userID!=$("#hidden").val() && v.agentID!=0)
                {
                    tr += "<tr>";
                    tr += "<td>" + v.loginName + "</a></td>";
                    tr += "<td>" + v.accountName + "</td>";
                    tr += "<td>" +v.upperLevelId+ "</td>";
                    tr += "<td class='text-center'>" + "代理分配" + "</td>";
                    tr += "<td class='text-center'>" + v.addDate + "</td>";
                    // if (v.is_last == 1) {
                    //     tr += "<td></td>";
                    // } else {
                    //     tr += "<td class='text-center'>" +
                    //         "<a href='javascript:void(0)' title='查询该用户的下级' onclick=\"agentObj.search('" + v.loginName + "')\"> " +
                    //         "<i class='fa  fa-arrow-circle-right'></i> " +
                    //         "</a>" +
                    //         "</td>";
                    // }
                    tr += "</tr>";
                }
            });

            if (tr == "") {
                tr = "<tr><td colspan='9' class='text-center'>--</td></tr>";
            }

            $("#agent_lists_table>tbody").html(tr);

        })
    },

    /**
     * 查询该账户下面的代理
     * @param account
     */
    search: function (account) {
        var obj = $('#agent_search_form');
        obj.find('select[name=level]').val(-1);
        obj.find('input[name=p_clientSN]').val(account);
        agentObj.lists(1, 1);
    },

    /**
     * 添加代理用户
     */
    addForm: function () {
        $.get('/agent/add-form', function (html) {
            $('#modal_pop_title').html('<i>添加下级代理</i>');
            $(document).off('click', '#modal_pop_btn');
            $(document).on('click', '#modal_pop_btn', function () {
                agentObj.add();
            });

            $('#modal_pop_body').html(html);
            $('#modal_pop').modal('show');
        })
    },

    /**
     * 提交添加
     * @returns {boolean}
     */
    add: function () {
        var params = {};
        var valid = true;
        $.each($("#agent_add_form").find("input,select"), function (i, v) {
            var name = $(v).attr('name');
            if (name) {
                var val = $(v).val();
                if (name != 'alias' && !val) {
                    new PNotify({
                        title: '错误提醒',
                        text: '必填项，不能为空',
                        delay: 5000,
                        styling: 'bootstrap3'
                    });

                    $(v).focus();
                    valid = false;
                    return false;
                }
                params[name] = val;
            }
        });

        if (!valid) return false;

        $.post('/agent/add', params, function (json) {
            var obj = {
                title: '操作通知',
                text: json.msg,
                styling: 'bootstrap3',
                delay: 5000,
                type: ''
            };
            if (json.code == 1) {
                $('#modal_pop').modal('hide');
                agentObj.lists(1, 1);

                obj.type = 'info';
                obj.addclass = 'dark';
            } else {
                obj.type = 'error';
            }
            new PNotify(obj);
        }, 'json');
    },

    /**
     * 修改用户信息
     * @param uid
     */
    setForm: function (uid) {
        $.get('/agent/set-form', {uid: uid}, function (html) {
            $('#modal_pop_title').html('<i>账户设置</i>');
            $('#modal_pop_body').html(html);
            $(document).off('click', '#modal_pop_btn');
            $(document).on('click', '#modal_pop_btn', function () {
                agentObj.set(uid);
            });

            $('#modal_pop').modal('show');

        })
    },

    /**
     * 提交修改
     * @param uid
     */
    set: function (uid) {
        var params = {};
        params.uid = uid;

        $.each($("#agent_set_form").find("input,select"), function (i, v) {
            var name = $(v).attr('name');
            if (name) {
                params[name] = $(v).val();
            }
        });
        swal({
            title: '确定修改吗?',
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: '确定',
            cancelButtonText: '取消'
        }).then(function () {
            $.post('/agent/set', params, function (json) {
                var obj = {
                    title: '操作通知',
                    text: json.msg,
                    styling: 'bootstrap3',
                    delay: 5000,
                    type: ''
                };
                if (json.code == 1) {
                    $('#modal_pop').modal('hide');
                    agentObj.lists(3, 0);

                    obj.type = 'info';
                    obj.addclass = 'dark';
                } else {
                    obj.type = 'error';
                }
                new PNotify(obj);
            }, 'json')
        }, function (dismiss) {

        });
    },
};
